package com.DHH.BikeAction;

import com.DHH.Config.ApiConstant;
import com.DHH.Config.DHHConstant;
import com.DHH.Util.Http;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Encoder;
import sun.security.provider.*;
import sun.security.provider.MD5;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 汪玉东 on 2017/8/25.
 */
public class DHHRequest {
    private Long timestamp=System.currentTimeMillis()/1000;
    private String sign;
    private String ctl;
    private String udid="";



    public BikeResult baseRequest(){
        MessageDigest md5= null;
        JSONObject jsonObject=null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            String str=DHHConstant.API_channel+ctl+timestamp+this.udid+DHHConstant.API_secret;
            //this.sign=base64en.encode(md5.digest(str.getBytes("utf-8")));
            this.sign=HEXAndMd5(str);
            Map<String,Object> parms=new HashMap<String, Object>();
            parms.put("timestamp",this.timestamp);
            parms.put("sign",this.sign);
            parms.put("ctl",this.ctl);
            parms.put("udid",this.udid);
            parms.put("secret",DHHConstant.API_secret);
            parms.put("channel",DHHConstant.API_channel);
            System.out.println(str);
            System.out.println(this.sign);
            Http http=new Http();
            String rs= http.sendGet(DHHConstant.API_URL,parms,"utf-8");
            jsonObject= JSON.parseObject(rs);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (Exception e) {
            //UnsupportedEncoding
            e.printStackTrace();
        }
        BikeResult bikeResult=new BikeResult();
       try{
           bikeResult.setCode(jsonObject.get("code").toString());
           bikeResult.setMsg(jsonObject.get("msg").toString());
       }catch (Exception e){

       }
        return  bikeResult;
    }


    public  String HEXAndMd5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            try {
                md.update(plainText.getBytes("UTF8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer(200);
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset] & 0xff;
                if (i < 16) buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getCtl() {
        return ctl;
    }

    public void setCtl(String ctl) {
        this.ctl = ctl;
    }

    public String getUdid() {
        return udid;
    }

    public void setUdid(String udid) {
        this.udid = udid;
    }
}
